<template>
  <el-row :gutter="20" class="list-page">
    <el-col
      :span="6"
      style="display: flex; flex-direction: column; height: 100%"
    >
      <el-tree
        class="tree"
        :data="bsnTree"
        :props="{ children: 'sons', label: 'label' }"
        :default-expand-all="true"
        :expand-on-click-node="false"
        @node-click="treeClick"
        highlight-current
      >
        <span slot-scope="{ data }">
          {{ data.label }}
        </span>
      </el-tree>
    </el-col>
    <el-col :span="18" style="height: 100%">
      <div class="data-list">
        <div class="data-list-header">
          <span style="margin-left: 20px">关键字</span>
          <el-input
            placeholder="请输入关键字"
            v-model="req.keyword"
            style="width: 200px; margin-left: 10px"
            clearable
          ></el-input>
          <el-button
            type="primary"
            style="margin-left: 20px"
            size="small"
            @click="getFlows()"
          >
            搜索
          </el-button>
          <el-button
            type="success"
            style="margin-left: 20px"
            size="small"
            @click="toAddFlow"
          >
            添加流程
          </el-button>
        </div>
        <div style="height: 80%">
          <el-table
            :data="res.Items"
            height="640"
            stripe
            border
            highlight-current-row
            @row-click="
              (row) => {
                company = row
              }
            "
          >
            <el-table-column
              label="业务"
              prop="BF_Name"
              width="160px"
            ></el-table-column>
            <el-table-column
              label="流程名称"
              prop="FLOW_Name"
              width="200px"
            ></el-table-column>
            <el-table-column label="描述" prop="FLOW_Desc"></el-table-column>
            <el-table-column label="创建时间">
              <template slot-scope="scope">
                {{ scope.row.FLOW_CreateTime | formatDate }}
              </template>
            </el-table-column>
            <el-table-column label="操作" width="200px">
              <template slot-scope="scope">
                <el-button type="text" @click="toEditFlow(scope.row.FLOW_ID)">
                  配置
                </el-button>
                <!-- status 10上线 20下线 -->
                <el-button
                  type="text"
                  @click="setFlowOnline(scope.row.FLOW_ID)"
                  v-if="scope.row.FLOW_Status == 10"
                >
                  上线
                </el-button>
                <el-button
                  type="text"
                  @click="setFlowOffline(scope.row.FLOW_ID)"
                  v-if="scope.row.FLOW_Status == 20"
                >
                  下线
                </el-button>
                <el-button type="text" @click="deleteFlow(scope.row.FLOW_ID)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            layout="total,prev,pager,next,sizes"
            style="float: right"
            background
            :current-page.sync="req.pageIndex"
            :total="res.Total"
            :page-size="req.pageSize"
            :page-sizes="[6, 10, 15]"
            @size-change="
              (val) => {
                req.pageSize = val
                getFlows()
              }
            "
            @current-change="getFlows()"
          ></el-pagination>
        </div>
      </div>
    </el-col>
  </el-row>
</template>
<script>
import {
  getFlows,
  getBusinessTree,
  setFlowOnline,
  setFlowOffline,
  deleteFlow,
} from '@/api/flowCenter/flow/flowManage'
export default {
  data: function () {
    return {
      bsnTree: [],
      req: {
        bsnId: null,
        keyword: '',
        pageIndex: 1,
        pageSize: 10,
      },
      res: {
        Items: [],
        Total: 0,
      },
    }
  },
  methods: {
    toAddFlow: function () {
      this.$router.push({
        name: 'editFlowPage',
      })
    },
    toEditFlow: function (id) {
      this.$router.push({
        name: 'editFlowPage',
        query: { id },
      })
    },
    treeClick(item) {
      switch (item.level) {
        case 0:
          this.req.bsnId = null
          break
        default:
          this.req.bsnId = item.id
          break
      }
      this.getFlows()
    },
    getBusinessTree() {
      getBusinessTree().then(({ data }) => {
        this.bsnTree = data
        this.getFlows()
      })
    },
    getFlows() {
      getFlows(this.req).then(({ data }) => {
        this.res = data
      })
    },
    setFlowOnline(id) {
      setFlowOnline(id).then(() => {
        this.getFlows()
      })
    },
    setFlowOffline(id) {
      setFlowOffline(id).then(() => {
        this.getFlows()
      })
    },
    deleteFlow(id) {
      deleteFlow(id).then(() => {
        this.getFlows()
      })
    },
  },
  mounted() {
    this.getBusinessTree()
  },
}
</script>

<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  padding-bottom: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  border-bottom: #f4f8ff 2px solid;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}
.main {
  height: calc(100% - 130px);
  padding: 10px;
}
.el-table::before {
  z-index: inherit;
}
</style>